package com.gdface.admin.gdface_attend.utils;


import android.util.Log;

import com.gdface.admin.gdface_attend.MyApplication;

import net.gdface.face.Att_logDao;
import net.gdface.face.Att_userDao;
import net.gdface.face.Entity.Att_log;
import net.gdface.face.Entity.Att_user;

import java.util.List;

/**
 * Created by admin on 2017/12/27.
 * <p>
 * 本地数据库操作
 */

public class DaoUtils {

    public static String updateTime_def = "******";

    public static void insertUser(Att_user entity) {
        getUserDao().insert(entity);
    }

    public static void deleteUser(Att_user entity) {
        getUserDao().delete(entity);
    }

    public static void deleteUserByKey(Long key) {
        getUserDao().deleteByKey(key);
    }

    public static void updateUser(Att_user entity) {
        getUserDao().update(entity);
    }

    public static List<Att_user> findUser() {
        return getUserDao().loadAll();
    }

    public static List<Att_user> findUserList(String userNO) {
        return getUserDao().queryBuilder().where(Att_userDao.Properties.UserNo.eq(userNO)).list();
    }

    public static Att_user findUser(Long key) {
        return getUserDao().load(key);
    }

    public static void insertLog(Att_log Att_log) {
        getLogDao().insert(Att_log);
    }

    public static void deleteLog(Att_log Att_log) {
        getLogDao().delete(Att_log);
    }

    public static void deleteLog(Long key) {
        getLogDao().deleteByKey(key);
    }

    public static void updateLog(Att_log Att_log) {
        getLogDao().update(Att_log);
    }

    public static List<Att_log> findLog() {
        return getLogDao().loadAll();
    }

    public static List<Att_log> findLoglist(String userNO) {
        return getLogDao().queryBuilder().where(Att_logDao.Properties.UserNo.eq(userNO)).orderDesc(Att_logDao.Properties.Attend_time).list();
    }

    public static Att_log findLog(Long key) {
        return getLogDao().load(key);
    }

    /**
     * 获取数据库中最大ID值
     *
     * @return
     */
    public static long getMaxId() {
        Log.i("hwwlog", "getMaxId()--------");
        List<Att_user> listuser = findUser();
        if (listuser.size() == 0) {
            return 1;
        } else {
            long num = listuser.get(0).getId();
            for (int i = 0; i < listuser.size(); i++) {
                num = (listuser.get(i).getId() < num ? num : listuser.get(i).getId());
            }
            return num + 1;
        }
    }

    /**
     * 获取userdao对象
     *
     * @return
     */
    private static Att_userDao getUserDao() {
        return MyApplication.getDaoSession().getAtt_userDao();
    }

    /**
     * 获取userdao对象
     *
     * @return
     */
    private static Att_logDao getLogDao() {
        return MyApplication.getDaoSession().getAtt_logDao();
    }
}
